package com.social.dao;

import com.social.model.LikeDislike;
import com.social.util.DBUtil;

import java.sql.*;
import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;

public class LikeDislikeDao {
    // 修复记录操作：正确处理状态变化
    public int recordAction(LikeDislike action) {
        String sql = "INSERT INTO like_dislike (log_id, user_id, action) VALUES (?, ?, ?) " +
                "ON DUPLICATE KEY UPDATE action = VALUES(action)";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setInt(1, action.getLogId());
            pstmt.setString(2, action.getUserId());
            pstmt.setInt(3, action.getAction());
            return pstmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
            return 0;
        }
    }
    public Map<String, Integer> getActionCounts(int logId) {
        Map<String, Integer> counts = new HashMap<>();
        counts.put("like", 0);
        counts.put("dislike", 0);

        String sql = "SELECT action, COUNT(*) as count " +
                "FROM like_dislike " +
                "WHERE log_id = ? " +
                "GROUP BY action";

        try (Connection conn = DBUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {

            pstmt.setInt(1, logId);
            ResultSet rs = pstmt.executeQuery();

            while (rs.next()) {
                int action = rs.getInt("action");
                int count = rs.getInt("count");

                if (action == 1) {
                    counts.put("like", count);
                } else if (action == -1) {
                    counts.put("dislike", count);
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return counts;
    }


    // 获取点赞数（确保只计1）
    public int getLikeCount(int logId) {
        String sql = "SELECT COUNT(*) FROM like_dislike WHERE log_id = ? AND action = 1";
        return getCount(sql, logId);
    }

    // 获取点踩数（确保只计-1）
    public int getDislikeCount(int logId) {
        String sql = "SELECT COUNT(*) FROM like_dislike WHERE log_id = ? AND action = -1";
        return getCount(sql, logId);
    }

    // 获取用户对日志的操作
    public int getUserAction(int logId, String userId) {
        String sql = "SELECT action FROM like_dislike WHERE log_id = ? AND user_id = ?";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setInt(1, logId);
            pstmt.setString(2, userId);
            ResultSet rs = pstmt.executeQuery();
            if (rs.next()) {
                return rs.getInt("action");
            }
            return 0; // 未操作
        } catch (SQLException e) {
            e.printStackTrace();
            return 0;
        }
    }

    // 通用计数方法
    private int getCount(String sql, int logId) {
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setInt(1, logId);
            ResultSet rs = pstmt.executeQuery();
            if (rs.next()) {
                return rs.getInt(1);
            }
            return 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return 0;
        }
    }
}